import { baseRequestClient } from '#/api/request'
import { requestClient } from '#/api/request'

// 查询维修记录基本信息列表
// 查询维修记录参数
interface MaintenanceItem {
  type?: number;
  rank?: number;
}
// 查询维修记录返回值
interface ListMaintenanceResult {
  rows: any;
  total: number;
}

//添加维修记录参数
export interface AddMaintenanceParams extends Record<string, any> {
  type: number;
  featureId: number;
  rank: number;
  remark: string;
  delFlag: number;
}

//修改维修记录参数
export interface EditMaintenanceParams extends Record<string,any> {
  type: number;
  featureId: number;
  rank: number;
  remark: string;
  delFlag: number;
}

//获取维修记录分页数据
export const listMaintenance = async (params: {
  pageNum: number;
  pageSize: number;
  type?: number;
  rank?: string;
}) => {
  const response = await requestClient.get<ListMaintenanceResult>('/manage/maintenance/list', { params });
  const data = response.rows; // 提取 rows 字段
  console.log("data:",data);
  let filteredData: MaintenanceItem[] = data;

  const total = response.total;
 
  const result = filteredData;

  //处理数据
  const typeMap: Record<number, string> = {
    0: '油水井',
    1: '计量间',
    2: '转油站',
    3: '管线',
    4: '阀池',
  };

  const mappedResult = result.map((row: any) => {
    return {
      ...row,
      typeFormatted: typeMap[row.type]  || '未知类型',
      rankFormatted: row.ranking === 1 ? '1级' : row.ranking === 2 ? '2级' : row.ranking === 3 ? '3级' : row.ranking === 4 ? '4级' : row.ranking === 5 ? '5级' : '未知等级',
    };
  });
  console.log("mappedResult:",mappedResult);
  

  return {
    items: mappedResult,
    total: total,
  };
};

// 查询维修记录基本信息详细
export async function getMaintenance(id:any) {
  return baseRequestClient.get(`/manage/maintenance/${id}`)
}

// 新增维修记录基本信息
export async function addMaintenance(data:AddMaintenanceParams) {
  await requestClient.post('/manage/maintenance', data);
}

// 修改维修记录基本信息
export async function updateMaintenance(data:EditMaintenanceParams) {
  requestClient.put('/manage/maintenance', data);
}

// 删除维修记录基本信息
export async function delMaintenance(id:number) {
  requestClient.delete(`/manage/maintenance/${id}`)
}

